package com.jdew.thkj.activity;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.utils.DeviceUtils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;
import com.scrat.app.selectorlibrary.ImageSelector;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by yangzai on 2018/4/10 0010.
 */

public class FanKuiActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.p1)
    ImageView p1;
    @Bind(R.id.p2)
    ImageView p2;
    @Bind(R.id.p3)
    ImageView p3;
    @Bind(R.id.p4)
    ImageView p4;
    @Bind(R.id.fankui)
    TextView fankui;
    @Bind(R.id.content)
    EditText content;
    private List<String> allpic = new ArrayList<>();
    private static final int REQUEST_CODE_SELECT_IMG = 1;
    private static final int MAX_SELECT_COUNT = 4;
    private int num = 0;

    @Override
    public int bindLayout() {
        return R.layout.activity_fankui;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        title.setText("意见反馈");
        p1.setImageResource(R.mipmap.add);
        back.setOnClickListener(this);
        p1.setOnClickListener(this);
        p2.setOnClickListener(this);
        p3.setOnClickListener(this);
        p4.setOnClickListener(this);
        fankui.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.p1: {
                ImageSelector.show(this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
            }
            break;
            case R.id.p2: {
                ImageSelector.show(this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
            }
            break;
            case R.id.p3: {
                ImageSelector.show(this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
            }
            break;
            case R.id.p4: {
                ImageSelector.show(this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
            }
            break;
            case R.id.fankui: {
                if (TextUtils.isEmpty(content.getText())) {
                    ToastUtils.getInstance(this).showMessage("请输入内容");
                    return;
                }
                if (!DeviceUtils.isNetworkAvalible(this)) {
                    ToastUtils.getInstance(this).showMessage("网络连接错误请检查");
                    return;
                }
                ToastUtils.getInstance(this).showMessage("提交成功");
                finish();
            }
            break;
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SELECT_IMG) {
            showContent(data);
            return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void showContent(Intent data) {
        allpic.addAll(ImageSelector.getImagePaths(data));
//        File  tempFile = new File(RegexValidateUtils.getFilePathFromUri(Uri.parse(ImageSelector.getImagePaths(data).get(0))));
        num = allpic.size();
        if (allpic.size() > 4) {
            for (int i = 0; i < num - 4; i++) {
                allpic.remove(0);
            }
        }

        if (allpic.isEmpty()) {
            return;
        }
        if (allpic.size() == 1) {
            p2.setImageResource(R.mipmap.add);
            Glide.with(FanKuiActivity.this).load(allpic.get(0)).into(p1);
            p1.setScaleType(ImageView.ScaleType.FIT_XY);
        } else if (allpic.size() == 2) {
            p3.setImageResource(R.mipmap.add);
            Glide.with(FanKuiActivity.this).load(allpic.get(0)).into(p1);
            Glide.with(FanKuiActivity.this).load(allpic.get(1)).into(p2);
            p1.setScaleType(ImageView.ScaleType.FIT_XY);
            p2.setScaleType(ImageView.ScaleType.FIT_XY);
        } else if (allpic.size() == 3) {
            p4.setImageResource(R.mipmap.add);
            Glide.with(FanKuiActivity.this).load(allpic.get(0)).into(p1);
            Glide.with(FanKuiActivity.this).load(allpic.get(1)).into(p2);
            Glide.with(FanKuiActivity.this).load(allpic.get(2)).into(p3);
            p1.setScaleType(ImageView.ScaleType.FIT_XY);
            p2.setScaleType(ImageView.ScaleType.FIT_XY);
            p3.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            Glide.with(FanKuiActivity.this).load(allpic.get(0)).into(p1);
            Glide.with(FanKuiActivity.this).load(allpic.get(1)).into(p2);
            Glide.with(FanKuiActivity.this).load(allpic.get(2)).into(p3);
            Glide.with(FanKuiActivity.this).load(allpic.get(3)).into(p4);
            p1.setScaleType(ImageView.ScaleType.FIT_XY);
            p2.setScaleType(ImageView.ScaleType.FIT_XY);
            p3.setScaleType(ImageView.ScaleType.FIT_XY);
            p4.setScaleType(ImageView.ScaleType.FIT_XY);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
